//---------------------------------------------------------------------------

#ifndef lsw100H
#define lsw100H
#include "Filter.h"

//---------------------------------------------------------------------------
#define HEART_CNT 3
#if 0
struct TCalc{
        TCalc()
        {
          m_k    = 1;
          m_zero = 0;
          m_ad   = 0;
          m_kg   = 0;
        }
        int setzero(int ad=0)
        {
            if(ad == 0)
                m_zero = m_ad;
            
            return m_zero;
        }

        double calckg(int ad)
        {
           m_diff = ad - m_zero;
           m_kg   = (m_diff)*m_k;
           m_ad   = ad;
           return m_kg;
        }
        double m_k;
        int  m_zero;
        int  m_ad;
        int  m_diff;
        double  m_kg;
};
struct Base{
      Base()
      {

      }
      void setzero()
      {
          for(int i = 0; i < 2; i++)
              calc[i].setzero();
      }
      double  calc_kg(int ad1, int ad2)
      {
            double kg = calc[0].calckg(ad1) + calc[1].calckg(ad2);
            kg = sysK * kg;
            return kg;
      }
      void setk1()
      {
           m_ad1 = calc[0].m_diff;
           m_ad2 = calc[1].m_diff;
      }
      void setk2()
      {
           m_ad3 = calc[0].m_diff;
           m_ad4 = calc[1].m_diff;
           
      }
      void set_sysK(double kg)
      {
            m_ad5 = calc[0].m_diff;
            m_ad6 = calc[1].m_diff;

            k2 = (kg*(m_ad1-m_ad3))/(m_ad1*m_ad4-m_ad2*m_ad3);
            k1 = (kg*(m_ad4-m_ad2))/(m_ad1*m_ad4-m_ad2*m_ad3);
            sysK = kg/(m_ad5*k1+m_ad6*k2);

            calc[0].m_k = k1;
            calc[1].m_k = k2;
      }
      TCalc calc[2];
      double sysK,k1,k2;

      int m_ad1,m_ad2;
      int m_ad3,m_ad4;
      int m_ad5,m_ad6;
};

struct filter{
    filter()
    {
         num = 0;
         filter_num = 16;
    }
    void setfilter(int num)
    {
         filter_num = num;
    }
    int filterit(double ad)
    {
       buf[num++] = ad;
       if(num> filter_num) num = 0;

       double v = 0;

       for(int i = 0; i < filter_num; i++)
       {
            v += buf[i];
       }
       return (v/ filter_num+0.5);
    }
    int filter_num;
    int num;
    double buf[64];
};
Base base;
 #endif
class lsw100
{
public:
  lsw100();
  double  calc_kg(int ad1, int ad2);
  bool load();
  bool save();
  void calib(int step,double kg=0);
  void set_zero();
  UINT32 filter(int ad1, int ad2);
  void update(double kg);
  void update2(double kg);
  void update3(double kg);
  double priv_calc_kg(int ad1,int ad2);
  double priv_calc_kg2(int ad1,int ad2);
public:
  TFilter* m_pFilter1;
  TFilter* m_pFilter2;
  TFilter* m_pFilter3;
  int m_cur_ad1,  m_cur_ad2;
  int m_diff_ad1, m_diff_ad2;
  int m_cur_zero1,    m_cur_zero2;
  double m_k,m_k1,m_k2;
  double m_kg1,m_kg2;
  double m_fama_kg;
  int m_calib1[2];
  int m_calib2[2];
  int m_calib3[2];
  
};

#endif
